package com.quik.util;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;

import com.quik.entity.SysAccount;

public class CommonUtils {

	public final static void setUpCrtMode(Object pojo, SysAccount account) {
		setUpCrtMode(pojo, account.getStaff_no());
	}

	public final static boolean validatePassword(String password, String encodedPassword){
		if(password==null) return false;
		return password.equals(encodedPassword);
	}
	
	public final static void setUpCrtMode(Object pojo, String staffNo) {
		try {
			Date cur = new Date();
			@SuppressWarnings("rawtypes")
			Class clazz = pojo.getClass();
			String getId = "getId";
			String setCrt_user = "setCrt_user";
			String setCrt_date = "setCrt_date";
			String setMod_user = "setMod_user";
			String setMod_date = "setMod_date";
			boolean ifAnyId = false;
			for (Method m : clazz.getMethods()) {
				if (getId.equals(m.getName())) {
					Object id = m.invoke(pojo);
					if(id!=null && id instanceof Integer){
						Integer idInt = new Integer(id.toString());
						if(idInt>0){
							ifAnyId = true;
						}
					}
				}
			}
			if(!ifAnyId){
				for (Method m : clazz.getMethods()) {
					if (setCrt_user.equals(m.getName())) {
						m.invoke(pojo, staffNo);
					}
					if (setCrt_date.equals(m.getName())) {
						m.invoke(pojo, cur);
					}
				}
			}
			for (Method m : clazz.getMethods()) {
				if (setMod_user.equals(m.getName())) {
					m.invoke(pojo, staffNo);
				}
				if (setMod_date.equals(m.getName())) {
					m.invoke(pojo, cur);
				}
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

	public final static Object queryOne(List<Object> list){
		Object rs = null;
		if(list!=null&&list.size()>0){
			rs = list.get(0);
		}
		return rs;
	}
}
